package com.penglecode.netty3.example3;

import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.SimpleChannelHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.penglecode.netty3.example2.ServerMessageHandler;

public class ServerApplicationCommandHandler extends SimpleChannelHandler {

	private static final Logger logger = LoggerFactory.getLogger(ServerMessageHandler.class);
	
	public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e)
			throws Exception {
		logger.error("[server] server internal error!", e.getCause());
		e.getChannel().close();
	}

	public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
		ApplicationCommand command = (ApplicationCommand) e.getMessage();
		logger.debug("[server] received message from client = {}", command);
		e.getChannel().write(command);
	}

	public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		logger.info("[server] a client channel closed!");
	}

	public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
		logger.info("[server] a client channel connected!");
	}

	public void channelDisconnected(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
		logger.info("[server] a client channel disconnected!");
	}
	
}
